//   Copyright 2012,2013 Vaughn Vernon
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
package com.saasovation.identityaccess.resource;

import com.saasovation.common.media.OvationsMediaType;
import com.saasovation.common.serializer.ObjectSerializer;
import com.saasovation.identityaccess.application.ApplicationServiceRegistry;
import com.saasovation.identityaccess.application.IdentityApplicationService;
import com.saasovation.identityaccess.application.command.OfferRegistrationInvitationCommand;
import com.saasovation.identityaccess.application.command.ProvisionTenantCommand;
import com.saasovation.identityaccess.application.representation.TenantRepresentation;
import com.saasovation.identityaccess.domain.model.identity.Tenant;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.jboss.resteasy.annotations.cache.Cache;

@Path("/tenants")
public class TenantResource {

    public TenantResource() {
        super();
    }

    @POST
    @Produces({OvationsMediaType.ID_OVATION_TYPE})
    @Consumes({MediaType.APPLICATION_JSON})
    public Response provisionTenant(ProvisionTenantCommand command) {
        Tenant tenant = this.identityApplicationService().provisionTenant(command);
        TenantRepresentation representation = new TenantRepresentation(tenant);
        String tenantRepresentation = ObjectSerializer.instance().serialize(representation);
        return Response.ok(tenantRepresentation).build();
    }

    @POST
    @Path("/invitations")
    @Produces({MediaType.TEXT_PLAIN})
    @Consumes({MediaType.APPLICATION_JSON})
    public Response offerRegistrationInvitation(OfferRegistrationInvitationCommand command) {
        String invitationId = this.identityApplicationService().offerRegistrationInvitation(command);
        return Response.ok(invitationId).build();
    }

    @GET
    @Path("{tenantId}")
    @Produces({OvationsMediaType.ID_OVATION_TYPE})
    @Cache(maxAge = 3600)
    public Response getTenant(@PathParam("tenantId") String aTenantId) {
        Tenant tenant = this.identityApplicationService().tenant(aTenantId);
        if (tenant == null) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        TenantRepresentation representation = new TenantRepresentation(tenant);
        String tenantRepresentation = ObjectSerializer.instance().serialize(representation);
        return Response.ok(tenantRepresentation).build();
    }

    private IdentityApplicationService identityApplicationService() {
        return ApplicationServiceRegistry.identityApplicationService();
    }

    public static void main(String[] args) {
        String json = ObjectSerializer.instance().serialize(new ProvisionTenantCommand(
                "All-Star Tenant",
                "An all-star company.",
                "Frank", "Oz",
                "frank@allstartcompany.com",
                "212-555-1211",
                "212-555-1212",
                "123 5th Avenue",
                "New York",
                "NY",
                "11201",
                "US"));
        System.out.println(json);
    }
}
